package com.example.homework9;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class SqliteBaseFragment extends Fragment {
    DbHelper dbHelper;
    private TextView contentTextView;
    public SqliteBaseFragment() {
        super(R.layout.sqlite_base);
    }

    private void showUsers() {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        StringBuilder stringBuilder = new StringBuilder();
        Cursor cursor = database.query("users",null, null,null,null,null,null);
        while (cursor.moveToNext()) {
            long id = cursor.getLong(cursor.getColumnIndex("id"));
            String username = cursor.getString(cursor.getColumnIndex("username"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            stringBuilder.append("id: ").append(id)
                    .append("  username: ").append(username)
                    .append("  age: ").append(age)
                    .append("\n");
        }
        contentTextView.setText(stringBuilder.toString());
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        dbHelper = new DbHelper(getContext());
        contentTextView = getView().findViewById(R.id.show);
        EditText nameEditText = getView().findViewById(R.id.edit_name);
        EditText ageEditText = getView().findViewById(R.id.edit_age);
        Button add = getView().findViewById(R.id.add);
        Button query = getView().findViewById(R.id.query);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username;
                int age;
                username = nameEditText.getText().toString();
                age = Integer.parseInt(ageEditText.getText().toString());
                SQLiteDatabase database = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("username",username);
                values.put("age",age);
                long ret = database.insert("users",null,values);
                String msg = ret == -1 ? "添加失败!" : "添加成功";
                Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
                database.close();

                if (msg.equals("添加成功")){
                    showUsers();
                }
            }
        });
        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase database = dbHelper.getReadableDatabase();
                StringBuilder stringBuilder = new StringBuilder();
                Cursor cursor = database.query("users",null, "age<18",null,null,null,null);
                while (cursor.moveToNext()) {
                    long id = cursor.getLong(cursor.getColumnIndex("id"));
                    String username = cursor.getString(cursor.getColumnIndex("username"));
                    int age = cursor.getInt(cursor.getColumnIndex("age"));
                    stringBuilder.append("id: ").append(id)
                            .append("  username: ").append(username)
                            .append("  age: ").append(age)
                            .append("\n");
                }
                contentTextView.setText(stringBuilder.toString());
            }
        });
    }
}
